﻿#ifndef CAMERA_H
#define CAMERA_H
#include <QObject>
//#include <QImage>
#include <QThread>
#include <QtConcurrent>
//#include <QMetaType>
//#include "camera_define.h"
//#include "axil_cmd.h"
#include "sensor_config.h"
//#include "sensor_user.h"
#include "sensor_spi.h"
//#include "libaxidma.h"
#include "waitsave.h"


class WaitSave;
//与相机开发板进行通信，设计相机功能实现
class Camera : public QObject
{
    Q_OBJECT

public:
    Camera(QObject *parent = nullptr);
    ~Camera();

    bool CameraInit();
    int CameraExternTrigger();
    int GetCameraInfo(CameraSpace::CameraInfo &info);
    int SetCameraMode(unsigned char mode, unsigned char &current_mode);
    int SetTriggerSource(unsigned char source, unsigned char &current_source);
    int TriggerOne(QList<QByteArray> &list);
    int TriggerMore(QList<QByteArray> &list);
    int TriggerStop();
//    int WaitSave(QList<QByteArray> *list);
    int SetExposureTime(quint16 time, quint16 &current_time);
    int SetFrame(quint16 frame, quint16 &current_frame);
    int SetAnologGain(unsigned char gain, unsigned char &current_gain);
//    int SetDigiGain(unsigned char gain, unsigned char &current_gain);
    int SetWindow(CameraSpace::CameraWindow window, CameraSpace::CameraWindow &current_window);
    int GetTemp(quint16 &temp);
    int getStorageSize();
    int getTemperature();
    int SetpicTMode(unsigned char mode, unsigned char &current_mode);
    int RecoverCamera(unsigned char &res);
    int SetSaveLocation(unsigned char mode, unsigned char &current_mode);
    int SetPhotoMessage(QByteArray photo_message, QByteArray &current_photo_message);
    void releaseMemory();
    bool DetectionExternalTrigger();
    void SetDeviceNum(const unsigned char k_device_num);
signals:
    void pictureOverTrans();
//    void beginSaveAndTrans();
public slots:
    void getListAdd(QList<QByteArray>* list);
    void TimeOutTrigger();

private:
    QByteArray IntToArray(int v);
    QByteArray ShortToArray(unsigned short v);
    bool cameraRestart();

    void sensor_spi_write(unsigned int TX_ADDR,unsigned int TX_DATA);
    unsigned int sensor_spi_read(unsigned int RX_ADDR);
private:
    //img
//    QImage image;
    CameraSpace::CameraInfo cameraInfo;
    unsigned int cameraIndex = 0;
    QByteArray photo_file_message_;
    char *rx_buf;
    axidma_dev_t axidma_dev;
    const array_t *rx_chans;
//    SaveThread *pworkThread;//存储照片的线程
    bool firstFlag = true;
    QThread* saveThread;
    unsigned char *image_mid0;
    WaitSave* waitSave;
    QList<QByteArray>* listTrans;
    QTimer* triggerTime;

    QTimer* dma_success_flag_time_;
    unsigned int timeOut = 1000;
    unsigned int dma_success_flag_timeout_ = 1;
    unsigned short exposureing_state_ = 1;

    unsigned int sensor_success_access_num_ = 0;

};

#endif // CAMERA_H
